﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WiseLinkWPF.Windows.Controls.Map;
using System.IO;

namespace CCF20.Shared
{
    public class ChinaMap : MapProviderBase
    {
        private const int TILE_SIZE = 256;
        private bool initialized;
        private string tileLocation;

        /// <summary> 
        /// Initializes a new instance of the FileSystemProvider class. 
        /// </summary> 
        /// <param name="mode">Map mode.</param> 
        /// <param name="labelVisible">Is labels visible.</param> 
        public ChinaMap(string tileLocation)
            : base(MapMode.Road, true)
        {
            this.tileLocation = tileLocation;
        }

        /// <summary> 
        /// Initializes a new instance of the FileSystemProvider class. 
        /// </summary> 
        public ChinaMap()
            : this(null)
        {
        }

        /// <summary> 
        /// Gets or sets location of the map tiles. 
        /// </summary> 
        public string TileLocation
        {
            get
            {
                return this.tileLocation;
            }

            set
            {
                this.tileLocation = value;
            }
        }

        /// <summary> 
        /// Gets the IsInitialized property. 
        /// Indicates that the provider is initialized. 
        /// </summary> 
        public override bool IsInitialized
        {
            get
            {
                return this.initialized;
            }
        }

        /// <summary> 
        /// Gets value which indicates whether labels are supported by the map provider. 
        /// </summary> 
        public override bool IsLabelSupported
        {
            get
            {
                return false;
            }
        }

        /// <summary> 
        /// Returns the SpatialReference for the map provider. 
        /// </summary> 
        public override ISpatialReference SpatialReference
        {
            get
            {
                return new MercatorProjection();
            }
        }

        /// <summary> 
        /// Gets list of the supported map modes. 
        /// </summary> 
        /// <returns>List of the supported map modes.</returns> 
        public override IEnumerable<MapMode> SupportedModes
        {
            get
            {
                yield return MapMode.Road;
            }
        }

        /// <summary> 
        /// Gets the image URI. 
        /// </summary> 
        /// <param name="tileLevel">Tile level.</param> 
        /// <param name="tilePositionX">Tile X.</param> 
        /// <param name="tilePositionY">Tile Y.</param> 
        /// <returns>URI of image.</returns> 
        public override Uri GetTile(int tileLevel, int tilePositionX, int tilePositionY)
        {
            int zoomLevel = ConvertTileToZoomLevel(tileLevel);

            
            string tileFileName = string.Format("os_{0}_{1}_{2}.png", tilePositionX, tilePositionY, zoomLevel);
            tileFileName = Path.Combine(this.TileLocation, tileFileName);

            if (File.Exists(tileFileName))
            {
                return new Uri(tileFileName);
            }
            else
            {
                return null;
            }
        }

        /// <summary> 
        /// Initialize provider. 
        /// </summary> 
        public override void Initialize()
        {
            this.initialized = base.IsInitialized;
        }

        /// <summary> 
        /// Gets value which indicates whether given mode is supported by map provider. 
        /// </summary> 
        /// <param name="mode">Map mode to check.</param> 
        /// <returns>true if given mode is supported. Otherwise - false.</returns> 
        public override bool IsModeSupported(MapMode mode)
        {
            return (mode == MapMode.Road);
        }

        /// <summary> 
        /// MapModeChanged handler. 
        /// </summary> 
        protected override void OnMapModeChanged(MapMode oldMode, MapMode newMode)
        {
            if (!this.IsSuspended)
            {
                this.Initialize();
            }
        }
    } 

}
